package com.clever.mall.controller;

import com.clever.mall.commons.dto.ShoppingCartDto;
import com.clever.mall.commons.result.BaseResult;
import com.clever.mall.service.ShoppingCartService;
import com.clever.mall.vo.ShoppingCartVo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @program: project-item
 * @author: hzx
 * @since: JDK 1.8
 * @create: 2020-11-10 10:13
 **/
@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {

    @Resource
    private ShoppingCartService service;

    /**
     * 通过用户id查询所有购物车内容
     * @param uid
     * @return
     */
    @GetMapping("all")
    public BaseResult<List<ShoppingCartVo>> selectAllShoppingCart(int uid) {
        return BaseResult.success(service.selectShoppingCartAll(uid));
    }

    /**
     * 分页查询,第一页为 1
     * @param uid
     * @param page  第几页
     * @param size  单页数量
     * @return
     */
    @GetMapping("limit")
    public BaseResult<List<ShoppingCartVo>> selectLimitShoppingCart(int uid, int page, int size) {
        return BaseResult.success(service.selectShoppingCartByLimit(uid,page,size));
    }

    /**
     * 将商品加入购物车
     * @param shoppingCartDto
     * @return
     */
    @PostMapping("add")
    public BaseResult<Integer> insertShoppingCart(@RequestBody ShoppingCartDto shoppingCartDto) {
        return BaseResult.success(service.insertShoppingCart(shoppingCartDto));
    }

    /**
     * 通过用户id和产品id删除购物车内容
     * @param uid
     * @param pid
     * @return
     */
    @PostMapping("del")
    public BaseResult<Integer> delShoppingCart(int uid, int pid) {
        return BaseResult.success(service.deleteShoppingCart(uid,pid));
    }

    //批量删除
    @PostMapping("delList")
    public BaseResult<Integer> delShoppingCartList(@RequestBody List<ShoppingCartDto> shoppingCartDtoList) {
        return BaseResult.success(service.deleteShoppingCartList(shoppingCartDtoList));
    }

}
